<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('设备列表')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: ztree-css" />
</head>
<body class="gray-bg">
    <div class="ui-layout-west">
        <div class="box box-main">
            <div class="box-header">
                <div class="box-title">
                    <i class="fa icon-grid"></i> 安装位置
                </div>
                <div class="box-tools pull-right">
                    <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i class="fa fa-chevron-up"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i class="fa fa-chevron-down"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新"><i class="fa fa-refresh"></i></button>
                </div>
                <div class="ui-layout-content">
                    <div id="tree" class="ztree"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="ui-layout-center">
        <div class="container-div">
            <div class="row">
                <div class="col-sm-12 search-collapse">
                    <form id="formId">
                        <input type="hidden" id="installId" name="installId">
                        <div class="select-list">
                            <ul>
                                <li>
                                    <label>设备编码：</label>
                                    <input type="text" name="deviceCode"/>
                                </li>
                                <li>
                                    <label>imei号：</label>
                                    <input type="text" name="deviceImei"/>
                                </li>
                                <li>
                                    <label>运营商：</label>
                                    <select name="operatorType" th:with="type=${@dict.getType('operator_type')}">
                                        <option value=""></option>
                                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                    </select>
                                </li>
                                <li>
                                    <label>设备厂家：</label>
                                    <select name="deviceFactory" th:with="type=${@dict.getType('device_factory')}">
                                        <option value=""></option>
                                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                    </select>
                                </li>
                                <li class="select-time">
                                    <label>资费到期</label>
                                    <input type="text" class="input-sm form-control" id="datetimepicker-startTime" name="params[beginTime]" placeholder="开始时间" th:value="${beginTime}"/>
                                    <span>-</span>
                                    <input type="text" class="input-sm form-control" id="datetimepicker-endTime" name="params[endTime]" placeholder="结束时间" th:value="${endTime}"/>
                                </li>
                                <li>
                                    <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                                </li>
                            </ul>
                        </div>
                    </form>
                </div>

                <div class="btn-group-sm" id="toolbar" role="group">
                    <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="basic:device:add">
                        <i class="fa fa-plus"></i> 新增
                    </a>
                    <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="basic:device:edit">
                        <i class="fa fa-edit"></i> 修改
                    </a>
                    <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="basic:device:remove">
                        <i class="fa fa-remove"></i> 删除
                    </a>
                    <a class="btn btn-info" onclick="$.table.importExcel()" shiro:hasPermission="basic:device:import">
                        <i class="fa fa-upload"></i> 导入
                    </a>
                    <a class="btn btn-default multiple disabled" onclick="reading()" shiro:hasPermission="basic:device:reading">
                        <i class="fa fa-edit"></i> 抄表
                    </a>
                </div>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: layout-latest-js" />
    <th:block th:include="include :: ztree-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('basic:device:edit')}]];
        var removeFlag = [[${@permission.hasPermi('basic:device:remove')}]];
        var prefix = ctx + "basic/device";

        var datas = [[${@dict.getType('operator_type')}]];
        var factorys = [[${@dict.getType('device_factory')}]];

        $(function() {
            var panehHidden = false;
            if ($(this).width() < 769) {
                panehHidden = true;
            }
            $('body').layout({ initClosed: panehHidden, west__size: 185 });
            // 回到顶部绑定
            if ($.fn.toTop !== undefined) {
                var opt = {
                    win:$('.ui-layout-center'),
                    doc:$('.ui-layout-center')
                };
                $('#scroll-up').toTop(opt);
            }

            queryHouseTree();
            queryDeviceList();
        });

        function queryDeviceList() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                importUrl: prefix + "/importData",
                importTemplateUrl: prefix + "/importTemplate",
                modalName: "采集设备",
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'deviceId',
                        title: 'id',
                        visible: false
                    },
                    {
                        field: 'deviceCode',
                        title: '设备编码'
                    },
                    {
                        field: 'installAddr',
                        title: '安装位置'
                    },
                    {
                        field: 'installTime',
                        title: '安装日期'
                    },
                    {
                        field: 'deviceImei',
                        title: 'sim卡号'
                    },
                    {
                        field: 'operatorType',
                        title: '运营商',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(datas, value);
                        }
                    },
                    {
                        field: 'deviceFactory',
                        title: '设备厂家',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(factorys, value);
                        }
                    },
                    {
                        field: 'endDate',
                        title: '资费到期'
                    },
                    {
                        field: 'meterCount',
                        title: '热表数量',
                        formatter: function (value, row, index){
                            return '<a href="javascript:void(0)" onclick="meter(\'' + row.deviceId + '\')">' + value + '</a>';
                        }
                    },
                    {
                        field: 'onlineStatus',
                        title: '在线状态',
                        formatter: function (value, item, index) {
                            if (value == 'online') {
                                return '<span class="label label-success">在线</span>';
                            } else {
                                return '<span class="label label-danger">离线</span>';
                            }
                        }
                    },
                    {
                        field: 'onlineTime',
                        title: '上线时间'
                    },
                    {
                        field: 'offlineTime',
                        title: '离线时间'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        formatter: function(value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.deviceId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.deviceId + '\')"><i class="fa fa-remove"></i>删除</a>');
                            return actions.join('');
                        }
                    }]
            };
            $.table.init(options);
        }

        function queryHouseTree() {
            var url = ctx + "basic/house/treeData";
            var options = {
                url: url,
                expandLevel: 1,
                onClick : zOnClick
            };
            $.tree.init(options);

            function zOnClick(event, treeId, treeNode) {
                $("#installId").val(treeNode.id);
                $.table.search();
            }
        }

        $('#btnExpand').click(function() {
            $._tree.expandAll(true);
            $(this).hide();
            $('#btnCollapse').show();
        });

        $('#btnCollapse').click(function() {
            $._tree.expandAll(false);
            $(this).hide();
            $('#btnExpand').show();
        });

        $('#btnRefresh').click(function() {
            queryHouseTree();
        });

        function meter(deviceId){
            var url = ctx + 'basic/meter/device/' + deviceId;
            $.modal.openTab("表信息", url);
        }

        /**
         * 数据采集
         */
        function reading(){
            var rows = $.table.selectFirstColumns();
            $.ajax({
                url: ctx + "data/meter/readingDevice",
                type: "post",
                dataType: "json",
                data: {"deviceIds" : rows.join() },
                beforeSend: function () {
                    $.modal.loading("正在发送抄表指令，请稍候...");
                    $.modal.disable();
                },
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }
    </script>
</body>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <input type="checkbox" id="updateSupport" name="updateSupport" title="如果采集器信息已经存在，更新这条数据。"> 是否更新已经存在的采集器信息
                &nbsp;	<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>
</html>